package cn.edu.nchu.fragmentdemo.view;

/**
 * @className: PostureBallView
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2023/10/07 19:35
 **/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;

/**
 * @author 16035
 */

public class PostureBallView extends View {
    /** 整个区域的半径 */
    private float ballRadius;

    private float DEC;

    /** 模拟的偏航角度*/
    private float angleYaw;

    /** 模拟滚转角度变化 */
    private float angleRoll;

    /** 模拟偏转角度变化 */
    private float anglePitch;

    private int i = 0;


    public PostureBallView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化数据
        angleYaw = -3;
        anglePitch = 0;
        angleRoll = 0;
        DEC = (float)Math.PI/180;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if ( i % 20 == 0 && angleRoll < 90 ) {
            angleRoll += 0.1;
        }
        else if ( i % 20 != 0 && angleRoll > -90 ) {
            angleRoll -= 0.1;
        }
        i++;
        angleYaw++;
        if( anglePitch < 90 ) {
            anglePitch += 0.1;
        }
        ballRadius = getWidth()/2-ballRadius*10/150;
        float centerX = getWidth() / 2;
        float centerY = getHeight() / 2;
        //主体部分
        drawBodyBall(canvas,angleRoll,anglePitch,centerX,centerY);
        // 添加静止部分
        drawStaticPart(canvas,centerX,centerY);
        //偏航方位显示
        drawYawPart(canvas,angleYaw,centerX,centerY);
        //边框样式
        drawBorderPart(canvas,centerX,centerY);
        //设置显示范围
        setCircularOutline(this,(int)ballRadius-5);

        // 重绘视图
        invalidate();
    }

    /** 设置整体显示范围 */
    public void setCircularOutline(View view, final int radius) {
        view.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                int centerX = view.getWidth() / 2;
                int centerY = view.getHeight() / 2;
                outline.setOval(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
            }
        });
        view.setClipToOutline(true);
    }

    /** 绘制外层边框 */
    public void drawBorderPart(Canvas canvas,float centerX,float centerY) {
        // 设置外围边框的颜色和宽度
        Paint borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setColor(Color.WHITE);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(6);
        canvas.drawCircle(centerX, centerY, ballRadius, borderPaint);
    }

    /** 姿态球主体部分绘制 偏转 滚转 */
    public void drawBodyBall(Canvas canvas,float angleRoll,float anglePitch,float centerX,float centerY) {
        // 姿态球主体部分绘制
        Paint circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.rgb(17,162,253));
        canvas.drawCircle(centerX, centerY, ballRadius, circlePaint);

        // 通过扇形圆心及角度的改变来模拟姿态球运动
        Paint arcPaint = new Paint();
        arcPaint.setAntiAlias(true);
        arcPaint.setColor(Color.rgb(255,168,3));
        //扇形限制区域的顶部
        float rectTopY = centerY - ballRadius;
        //扇形限制区域的底部
        float rectBottomY = centerY + ballRadius;
        if ( anglePitch >= 0 && anglePitch <= 90 ) {
            rectBottomY = centerY + ballRadius;
            rectTopY -= ballRadius/90 * anglePitch*2;
        }
        else if ( anglePitch < 0 && anglePitch > -90 ) {
            rectTopY = centerY - ballRadius;
            rectBottomY += Math.abs(ballRadius/90 * anglePitch*2);
        }
        RectF rectF = new RectF(centerX - ballRadius*2, rectTopY, centerX + ballRadius*2, rectBottomY);
        canvas.drawArc(rectF, angleRoll, 180, true, arcPaint);
    }

    /** 绘制偏航的方位*/
    public void drawYawPart(Canvas canvas,float angleYaw,float centerX,float centerY) {
        // 偏航显示
        // 四个方位的坐标
        float radiusOfYaw = ballRadius-ballRadius*25/150;
        //处理yaw四个方位圆心的偏移
        float yaw_centerX = centerX - ballRadius*12/150;
        float yaw_centerY = centerY + ballRadius*4/150;
        // 通过角度换算四个方位的坐标
        float xOfNorth = yaw_centerX + (float) (radiusOfYaw * Math.sin(Math.toRadians(angleYaw)));
        float yOfNorth = yaw_centerY - (float) (radiusOfYaw * Math.cos(Math.toRadians(angleYaw)));
        float xOfWest = yaw_centerX + (float) (radiusOfYaw * Math.sin(Math.toRadians(angleYaw - 90)));
        float yOfWest = yaw_centerY - (float) (radiusOfYaw * Math.cos(Math.toRadians(angleYaw - 90)));
        float xOfSouth = yaw_centerX + (float) (radiusOfYaw * Math.sin(Math.toRadians(angleYaw +180)));
        float yOfSouth = yaw_centerY - (float) (radiusOfYaw * Math.cos(Math.toRadians(angleYaw +180)));
        float xOfEast = yaw_centerX + (float) (radiusOfYaw * Math.sin(Math.toRadians(angleYaw + 90)));
        float yOfEast = yaw_centerY - (float) (radiusOfYaw * Math.cos(Math.toRadians(angleYaw + 90)));
        // 绘制四个方位
        Paint bluePaint = new Paint();
        bluePaint.setAntiAlias(true);
        bluePaint.setColor(Color.BLACK);
        bluePaint.setTextSize(ballRadius*3/15);
        bluePaint.setStrokeWidth(ballRadius*5/15);
        canvas.drawText("N", xOfNorth, yOfNorth, bluePaint);
        canvas.drawText("W", xOfWest, yOfWest, bluePaint);
        canvas.drawText("S", xOfSouth, yOfSouth, bluePaint);
        canvas.drawText("E", xOfEast, yOfEast, bluePaint);

        angleYaw += 1;
        if ( angleYaw > 360 ) {
            angleYaw = 0;
        }

    }

    /** 绘制静止部分 */
    public void drawStaticPart(Canvas canvas,float centerX,float centerY) {
        Paint arcPaint = new Paint();
        arcPaint.setAntiAlias(true);
        // 画圆弧
        arcPaint.setColor(Color.WHITE);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(ballRadius/75);
        RectF arcRect = new RectF(centerX - ballRadius*11/15, centerY - ballRadius*11/15, centerX + ballRadius*11/15, centerY + ballRadius*11/15);
        canvas.drawArc(arcRect, -150, 120, false, arcPaint);

        // 画刻度
        Paint linePaint = new Paint();
        arcPaint.setAntiAlias(true);
        linePaint.setColor(Color.WHITE);
        linePaint.setStrokeWidth(ballRadius/75);

        //上横刻度
        canvas.drawLine(centerX-ballRadius/3, centerY-ballRadius*2/15, centerX+ballRadius/3, centerY-ballRadius*2/15, linePaint);
        canvas.drawLine(centerX-ballRadius/5, centerY-ballRadius*4/15, centerX+ballRadius/5, centerY-ballRadius*4/15, linePaint);
        canvas.drawLine(centerX-ballRadius/3, centerY-ballRadius*6/15, centerX+ballRadius/3, centerY-ballRadius*6/15, linePaint);

        float angle = 40*DEC;
        //上小刻度
        while ( angle < 149*DEC ) {
            canvas.drawLine((float)(centerX-(ballRadius*11/15)*Math.cos(angle)), (float)(centerY-(ballRadius*11/15)*Math.sin(angle)),
                    (float)(centerX-(ballRadius*10/15)*Math.cos(angle)), (float)(centerY-(ballRadius*10/15)*Math.sin(angle)), linePaint);
            angle+=10*DEC;
        }
        //底部刻度
        angle = 240*DEC;
        while ( angle < 310*DEC ) {
            canvas.drawLine((float)(centerX-(ballRadius*10/15)*Math.cos(angle)), (float)(centerY-(ballRadius*10/15)*Math.sin(angle)),
                    (float)(centerX-(ballRadius*9/15)*Math.cos(angle)), (float)(centerY-(ballRadius*9/15)*Math.sin(angle)), linePaint);
            angle+=10*DEC;
        }

        //下横刻度
        canvas.drawLine(centerX-ballRadius/3, centerY+ballRadius*2/15, centerX+ballRadius/3, centerY+ballRadius*2/15, linePaint);
        canvas.drawLine(centerX-ballRadius*3/15, centerY+ballRadius*4/15, centerX+ballRadius*3/15, centerY+ballRadius*4/15, linePaint);
        canvas.drawLine(centerX-ballRadius/3, centerY+ballRadius*6/15, centerX+ballRadius/3, centerY+ballRadius*6/15, linePaint);

        //中间两侧红色横线
        linePaint.setColor(Color.RED);
        linePaint.setStrokeWidth(ballRadius/30);

        canvas.drawLine(centerX-ballRadius*8/15, centerY, centerX-ballRadius*4/15, centerY, linePaint);
        canvas.drawLine(centerX+ballRadius*8/15, centerY, centerX+ballRadius*4/15, centerY, linePaint);

        // 绘制这个三角形,你可以绘制任意多边形
        Path path = new Path();
        // 此点为多边形的起点
        path.moveTo(centerX, centerY+ballRadius/15);
        path.lineTo(centerX-ballRadius*2/15, centerY+ballRadius/30);
        path.lineTo(centerX, centerY-ballRadius/10);
        path.lineTo(centerX+ballRadius*2/15, centerY+ballRadius/30);
        // 使这些点构成封闭的多边形
        path.close();


        canvas.drawPath(path, linePaint);
    }

}
